package club.xiaojiawei.binarytree;

import java.util.Deque;
import java.util.LinkedList;

/**
 * @author 肖嘉威
 * @version 1.0
 * @date 5/17/22 7:58 PM
 * @question 98. 验证二叉搜索树
 * @description 给你一个二叉树的根节点 root ，判断其是否是一个有效的二叉搜索树。
 * 有效 二叉搜索树定义如下：
 * 节点的左子树只包含 小于 当前节点的数。
 * 节点的右子树只包含 大于 当前节点的数。
 * 所有左子树和右子树自身必须也是二叉搜索树。

 */
public class IsValidBST98 {

    public static void main(String[] args) {
        IsValidBST98 test = new IsValidBST98();
        TreeNode root = new TreeNode(3,
                new TreeNode(1, new TreeNode(0), new TreeNode(2)),
                new TreeNode(5, new TreeNode(4), new TreeNode(6)));
        boolean result = test.isValidBST(root);
        System.out.println(result);
    }

    TreeNode root;

    /**
     * 遍历二叉树+搜索
     * 遍历二叉树，对二叉树里的每个节点在树中搜索，如果搜索不到说明不是排序树
     * 时间复杂度 O(nlogn)
     * @param root
     * @return
     */
    public boolean isValidBST(TreeNode root) {
        this.root = root;
        return recursion(root);
    }

    public boolean recursion(TreeNode node){
        if (node == null){
            return true;
        }
        if (search(node.val) != node){
            return false;
        }
        return recursion(node.left) && recursion(node.right);
    }

    public TreeNode search(int val){
        TreeNode temp = root;
        while (temp != null){
            if (temp.val == val){
                return temp;
            }
            temp = val > temp.val? temp.right : temp.left;
        }
        return null;
    }

    /**
     * 官方-递归(取边界)
     * 时间复杂度 O(n)
     * @param root
     * @return
     */
    public boolean isValidBST2(TreeNode root) {
        return isValidBST(root, Long.MIN_VALUE, Long.MAX_VALUE);
    }

    public boolean isValidBST(TreeNode node, long lower, long upper) {
        if (node == null) {
            return true;
        }
        if (node.val <= lower || node.val >= upper) {
            return false;
        }
        return isValidBST(node.left, lower, node.val) && isValidBST(node.right, node.val, upper);
    }

    /**
     * 官方-中序遍历（取最小值）
     * 时间复杂度 O(n)
     * @param root
     * @return
     */
    public boolean isValidBST3(TreeNode root) {
        Deque<TreeNode> stack = new LinkedList<TreeNode>();
        double inorder = -Double.MAX_VALUE;
        while (!stack.isEmpty() || root != null) {
            while (root != null) {
                stack.push(root);
                root = root.left;
            }
            root = stack.pop();
            // 如果中序遍历得到的节点的值小于等于前一个 inorder，说明不是二叉搜索树
            if (root.val <= inorder) {
                return false;
            }
            inorder = root.val;
            root = root.right;
        }
        return true;
    }



    static class TreeNode{

        private int val;

        private TreeNode left;

        private TreeNode right;

        public TreeNode() {
        }

        public TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }

        public TreeNode(int val) {
            this.val = val;
        }

        public int getVal() {
            return val;
        }

        public void setVal(int val) {
            this.val = val;
        }

        public TreeNode getLeft() {
            return left;
        }

        public void setLeft(TreeNode left) {
            this.left = left;
        }

        public TreeNode getRight() {
            return right;
        }

        public void setRight(TreeNode right) {
            this.right = right;
        }
    }
}
